﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CS_work_2
{
    public partial class CS_Input : UserControl
    {
        public CS_Input()
        {
            InitializeComponent();
        }
        private List<Label> nameLabels = new List<Label>();
        public List<Label> binaryLabels = new List<Label>();
        private List<CheckBox> checkBoxes = new List<CheckBox>();
        private Label hexLabel = new Label();

        private bool operate = false;
        private bool hexShow = false;

        public bool HexShow
        {
            get { return hexShow; }
            set { hexShow = value; }
        }


        public bool Operate
        {
            get { return operate; }
            set { operate = value; }
        }

        private string title;
        public string Title
        {
            get { return title; }
            set {
                this.label1.Text = value;
                title = value;
            }
        }

        public void initHexLabel() {
            this.hexLabel.Text = "00";
            this.hexLabel.Font = new Font("宋体", 20, FontStyle.Bold);
        }


        public void init(List<string> labels) {
            this.initHexLabel();
            this.label1.Height = 20;
            this.flowLayoutPanel2.Height = 20;
            if (this.HexShow)
            {
                this.Width = labels.Count * 30 + 120;
            }
            else {
                this.Width = labels.Count * 30 + 10;

            }
            if (operate)
            {
                this.Height = 3 * 30;
            }
            else {
                this.Height = 2 * 30 + 5;
            }
            
            foreach (string l in labels) {
                Label label = new Label();
                label.Width = 25;
                label.Height = 20;
                label.TextAlign = ContentAlignment.MiddleCenter;
                label.Text = l;
                nameLabels.Add(label);
                this.labelFlowPanel.Height = 20;
                this.labelFlowPanel.ForeColor = Color.Red;
                this.labelFlowPanel.Controls.Add(label);
            }
            for (int i = 0; i < labels.Count; i++) {
                Label label = new Label();
                label.Width = 25;
                label.Height = 20;
                label.Text = "0";
                label.TextAlign = ContentAlignment.MiddleCenter;
                binaryLabels.Add(label);
                this.binaryShowFlowPanel.ForeColor = Color.Green;
                this.binaryShowFlowPanel.Height = 20;
                this.binaryShowFlowPanel.Controls.Add(label);
            }
            foreach (string _ in labels)
            {
                CheckBox checkbox = new CheckBox();
                checkbox.Width = 25;
                checkbox.CheckAlign = ContentAlignment.MiddleCenter;
                checkbox.Height = 20;
                checkbox.CheckedChanged += onCheck;
                this.inputFlowPanel.Controls.Add(checkbox);
                checkBoxes.Add(checkbox);
            }
            if (this.HexShow) {
                this.flowLayoutPanel4.Controls.Add(hexLabel);
            }

        }
        public string getValue() {
            string res = "";
            foreach (Label l in this.binaryLabels) {
                res += l.Text;
            }
            return res;
        }
        public void setValue(string s)
        {
            if (s.Length > this.binaryLabels.Count) return;
            string res = reverString(s);
            this.hexLabel.Text = Convert.ToString(Convert.ToInt32(res, 2), 16).ToUpper();
            for (int i = 0; i < s.Length; i++) {
                this.binaryLabels[i].Text = s[i] + "";
            }
        }
        public void onCheck(object sender, EventArgs e) {
            for (int i = 0; i < checkBoxes.Count; i ++) {
                switch (checkBoxes[i].CheckState)
                {
                    case CheckState.Checked:
                        binaryLabels[i].Text = "1";
                        break;
                    case CheckState.Unchecked:
                        binaryLabels[i].Text = "0";
                        break;
                    case CheckState.Indeterminate:
                        // Code for indeterminate state.  
                        break;
                }
            }
        }
        public string reverString(string s)
        {
            string res = "";
            for (int i = s.Length - 1; i >= 0; i--)
            {
                res += s[i];
            }
            return res;
        }
        // 往后面加零
        public string fillZero(string s, int count)
        {
            for (int i = s.Length; i < count; i++)
            {
                s += "0";
            }
            return s;
        }
    }
}
